package dkbta.ontology.elements.repeatingpattern;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import dkbta.ontology.elements.Element;
import dkbta.ontology.elements.ElementType;
import dkbta.util.time.TimeInterval;

public class RepeatingPattern extends Element{
	/** The instances of the elements the pattern consists of */
	private final Element[] _elements;
	
	/**
	 * Initializes a repeating pattern instance with the given parameters
	 * 
	 * @param name The repeating pattern's name
	 * @param startTime The repeating pattern's start time
	 * @param endTime The repeating pattern's end time
	 * @param elements The elements the pattern consists of
	 */
	public RepeatingPattern(String name, long startTime, long endTime, Element[] elements){
		super(ElementType.REPEATINGPATTERN, name, startTime, endTime, null, null);
		_elements = elements;
	}

	/**
	 * Initializes a repeating pattern instance with the given parameters
	 * 
	 * @param name The repeating pattern's name
	 * @param timeInterval The repeating pattern's time interval
	 */
	public RepeatingPattern(String name, TimeInterval timeInterval, Element[] elements){
		super(ElementType.REPEATINGPATTERN, name, timeInterval, null, null);
		_elements = elements;
	}

	@Override
	public void insertDataIntoPS(PreparedStatement ps, int valueIndex, int typeIndex)
			throws SQLException{
		ps.setString(valueIndex, "true");
		ps.setString(typeIndex, "REPEATINGPATTERN");
	}
	
	@Override
	public String toString(){
		if (_elements == null){
			return super.toString();
		}else{
			StringBuilder sb = new StringBuilder(super.toString());
			sb.append(" - ").append(_elements[0]);
			for (int i = 1; i < _elements.length; ++i){
				sb.append(" | ");
				sb.append(_elements[i]);
			}

			return sb.toString();
		}
	}
}
